//
//  JSTSetupViewController.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/25.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTSetupViewController.h"
#import "JSTUserDataViewController.h"

@interface JSTSetupViewController ()<UITableViewDelegate, UITableViewDataSource,JSTAlertViewDelegate>

@property (nonatomic, strong) UITableView *tabView;

@property (nonatomic, strong) NSMutableArray *setUpData;

@property (nonatomic, strong) NSString *clearCacheStr;

@property (nonatomic, strong) NSString *logOutStr;

@end

@implementation JSTSetupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"设 置";
    
    [self createData];
    
    [self createContent];
}

- (void) createData
{
    _setUpData = [NSMutableArray array];
    [_setUpData addObject:@"账户资料"];
    [_setUpData addObject:@"更换手机号"];
    _clearCacheStr = @"清理缓存";
    _logOutStr = @"退出账号";
}

- (void) createContent
{
    [self.view addSubview:self.tabView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return _setUpData.count;
    }else if (section == 1)
    {
        return 1;
    }else
    {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setupViewCell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"setupViewCell"];
    }if (indexPath.section == 0)
    {
        cell.textLabel.text = [_setUpData safeObjectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if (indexPath.section == 1)
    {
        cell.textLabel.text = _clearCacheStr;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if (indexPath.section == 2)
    {
        cell.textLabel.text = _logOutStr;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = kRedColor;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            JSTUserDataViewController *userDataViewController = [[JSTUserDataViewController alloc] init];
            [self.navigationController pushViewController:userDataViewController animated:YES];
        }else if (indexPath.row == 1)
        {
            JSTAlertView *advertVeiw = [[JSTAlertView alloc] initWithTitle:@"提 示" message:@"敬请期待" customView:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [advertVeiw show];
        }
    }else if (indexPath.section == 1)
    {
        if ([[SDImageCache sharedImageCache] getSize] > 0)
        {
            CGFloat cacheSize = [[SDImageCache sharedImageCache] getSize]/1024./1024.;
            JSTAlertView *advertVeiw = [[JSTAlertView alloc] initWithTitle:@"提 示" message:[NSString stringWithFormat:@"确定清理%.2fM缓存吗", cacheSize] customView:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
            [advertVeiw setConfirmBlock:^
             {
                 [self displayOverFlowActivityView];
                 [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                     [self removeOverFlowActivityView];
                     [self presentSheet:@"清理完成"];
                 }];
             }];
            [advertVeiw show];
        }else
        {
            JSTAlertView *advertView = [[JSTAlertView alloc] initWithTitle:@"提 示" message:@"已经没有缓存了！" customView:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [advertView show];
        }
    }else
    {
        JSTAlertView *advertView = [[JSTAlertView alloc] initWithTitle:@"" message:@"是否退出当前账号" customView:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
        [advertView setConfirmBlock:^
        {
            NSUserDefaults *userDf = [NSUserDefaults standardUserDefaults];
            [userDf removeObjectForKey:@"loginToken"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            DLog(@"------ %@", [JSTSystemInfo loginToken]);
        }];
        [advertView show];
    }
}

//cell 分割线到头
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (UITableView *)tabView
{
    if (!_tabView) {
        _tabView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tabView.delegate = self;
        _tabView.dataSource = self;
    }
    return _tabView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
